package com.itita.weibo.ui;

import java.util.HashMap;
import java.util.List;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.WeiboUtil;

import weibo4andriod.User;
import weibo4andriod.Weibo;
import weibo4andriod.WeiboException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 用户信息
 * 
 * @author 焦明 
 *  
 *  2010-5-10 4:40 将当前查看用户信息任务改为添加到任务队列的对头
 */
public final class UserInfoActivity extends Activity implements IWeiboActivity{
   
	public static final int REFRESH = 1;
	
	public static final int REFRESH_USERICON = 2;
	
	private LinearLayout processLayout;
	
	private ImageView ivPortrait;
	
	private TextView tvNick;
	
	private Button btnEdit;
	
	private TextView tVAddress;
	
	private TextView tVAccountInfo;
	
	private TextView tVAttentionCount;
	
	private TextView tVWeiboCount;

	private TextView tVFansCount;
	
	private TextView tvTopicCount;
	
	private TextView tvFavCount;
	
	private TextView tvBlacklist;
	
	private View progress4UserInfo;
	
	
//	private View title;
//	
//	private Button btnNewBlog;
//	
//	private Button btnRefUserInfo;
//	
//	private TextView tvTitle;
	
	//关注
	private View vAllAttention;
	
	//微博
	private View vAllPostWeibos;
	
	//粉丝
	private View vAllFans;
	
	//话题
	private View vAllTopics;
	
	//收藏
	private View rlFav;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.userinfo);
        MainService.addActivity(this);
        this.initControlsAndaddControlsEvent();
        init();
    }

	private void initControlsAndaddControlsEvent() {
	    
		View title  = this.findViewById(R.id.title4UserInfo);
		
		Button btnNewBlog = (Button) title.findViewById(R.id.title_bt_left);
		
		Button btnRefUserInfo=(Button)title.findViewById(R.id.title_bt_right);
		
		TextView tvTitle = (TextView)title.findViewById(R.id.textView);
		tvTitle.setText(R.string.myUserInfo);
		
//		 final ImageView imageViewTitle = (ImageView) title.findViewById(R.id.imageViewTitle);
//		 imageViewTitle.setImageResource(R.drawable.my_user_info);
		 
		this.progress4UserInfo = this.findViewById(R.id.progress4UserInfo);
		
	    this.ivPortrait = (ImageView) this.findViewById(R.id.ivPortrait);
	    
	    this.tvNick = (TextView) this.findViewById(R.id.tvNick);
	    
	    this.btnEdit = (Button) this.findViewById(R.id.btnEdit);
	    
	    this.tVAddress = (TextView) this.findViewById(R.id.tvAddress_content);
		
	    this.tVAccountInfo = (TextView) this.findViewById(R.id.tvAccount_info_content);
		
	    this.tVAttentionCount = (TextView) this.findViewById(R.id.tvAttention_count);
		
	    this.tVWeiboCount = (TextView) this.findViewById(R.id.tvWeiboCount);

	    this.tVFansCount = (TextView) this.findViewById(R.id.tvFansCount);
		
	    this.tvTopicCount = (TextView) this.findViewById(R.id.tvTopicCount);
		
	    this.tvFavCount = (TextView) this.findViewById(R.id.tvFav_Count);
		
	    this.tvBlacklist = (TextView) this.findViewById(R.id.tvBlacklist);
	    
//	    this.tvTitle = (TextView) this.title.findViewById(R.id.textView);
	    
//	    this.tvTitle.setText(R.string.myUserInfo);
	    
	    this.vAllAttention = this.findViewById(R.id.allAttention);
	   
	    this.vAllPostWeibos = this.findViewById(R.id.allPostWeibos);
	    
	    this.vAllFans = this.findViewById(R.id.allFans);
	    
	    this.vAllTopics = this.findViewById(R.id.allTopics);
	    
	    this.rlFav = this.findViewById(R.id.rlFav);
	    btnEdit.setVisibility(View.GONE);
	    
	    this.btnEdit.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
			   Toast.makeText(UserInfoActivity.this, "编辑", Toast.LENGTH_SHORT)
			   .show();
			   
			}
	    });
	    
	    btnNewBlog.setVisibility(View.GONE);    
//    btnNewBlog.setOnClickListener(new OnClickListener(){
// 	        
//			public void onClick(View v) {
//		    //进入发表微博窗口
// 	    	  Intent itnewblog=new Intent(UserInfoActivity.this,NewWeiboActivity.class);
// 	    	  UserInfoActivity.this.startActivityForResult(itnewblog, 1);
// 	       }
//	});
	 
    btnNewBlog.setBackgroundResource(R.drawable.title_new);
    
    btnRefUserInfo.setBackgroundResource(R.drawable.title_reload);
    
   btnRefUserInfo.setOnClickListener(new OnClickListener(){     
		 public void onClick(View arg0) {
	     		//添加获取首页信息任务
    	    init();
    	    UserInfoActivity.this.progress4UserInfo.setVisibility(View.VISIBLE);
		}
    }); 
    //为查看关注 添加和取消关注添加事件 
    this.vAllAttention.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		    Toast.makeText(UserInfoActivity.this, "查看关注", Toast.LENGTH_SHORT).show();
		    Intent intent = new Intent(UserInfoActivity.this, ListAllFriendsActivity.class);
		    startActivityForResult(intent, 0);
		}
	});
    
    //查看微博
    this.vAllPostWeibos.setOnClickListener(new OnClickListener() {
    	
    	@Override
    	public void onClick(View v) {
   		Toast.makeText(UserInfoActivity.this, "查看我的所有微博", Toast.LENGTH_SHORT).show();
    		Intent intent = new Intent(UserInfoActivity.this, ListMyWeibosActivity.class);
 		    startActivityForResult(intent, 1);
    	}
    });
    
    //查看粉丝
    this.vAllFans.setOnClickListener(new OnClickListener() {
    	
    	@Override
    	public void onClick(View v) {
//    		Toast.makeText(UserInfoActivity.this, "查看关注", Toast.LENGTH_SHORT).show();
    		Intent intent = new Intent(UserInfoActivity.this, ListMyFansActivity.class);
 		    startActivityForResult(intent, 2);
    	}
    });
    
    this.vAllTopics.setOnClickListener(new OnClickListener() {
    	
    	@Override
    	public void onClick(View v) {
    		Toast.makeText(UserInfoActivity.this, "查看我的主题", Toast.LENGTH_SHORT).show();
    		Intent intent = new Intent(UserInfoActivity.this, ListMyTopicsActivity.class);
 		    startActivityForResult(intent, 3);
    	}
    });
    
    this.rlFav.setOnClickListener(new OnClickListener() {
    	
    	@Override
    	public void onClick(View v) {
    		Toast.makeText(UserInfoActivity.this, "查看我的收藏", Toast.LENGTH_SHORT).show();
    		Intent intent = new Intent(UserInfoActivity.this, ListMyFavoriteActivity.class);
    		startActivityForResult(intent, 4);
    	}
    });
    
    
}
    
	@Override
	protected void onResume() {
		super.onResume();
//		init();
	}
	
	/**
	 * 焦明
	 */
	public void init() {
		Task task = new Task(Task.TASK_GET_USER_INFO);
		//将当前任务添加任务队列的对头
		MainService.newTaskToTaskQueneHead(task);
	}

	public void refresh(Object... params) {
	    switch ((Integer)params[0]) {
		case UserInfoActivity.REFRESH:
			this.progress4UserInfo.setVisibility(View.GONE);
			if(params[1] == null){
//				if(MainService.isVerifySucc){
					WeiboUtil.myToastShow(this, "暂无数据");
					return;
//				}
			}
			
			User user = (User) params[1];
			this.tVAddress.setText(user.getLocation());
			this.tvNick.setText(user.getName());
		    this.tVAccountInfo.setText(MainService.mainService.weibo.getUserId()); 
//		    List<User> allFriends;
			try {
//				allFriends = MainService.mainService.weibo.getFriendsStatuses();
//				user.getFriendsCount()
//				  ////System.out.println("朋友数：" + user.getFriendsCount());
//				  ////System.out.println("粉丝数:" + user.getFollowersCount());
//				  
//				    ////System.out.println("--------------------");
//				    System.err.println("微博数:" + user.getStatusesCount());
//				    ////System.out.println("--------------------");
////				    ////System.out.println("话题数:" +user.getTrendscounts());
//				   
//				    ////System.out.println("收藏数:" + user.getFavouritesCount());
				    
				this.tVAttentionCount.setText(String.valueOf(user.getFriendsCount()));
			    this.tVWeiboCount.setText(String.valueOf(user.getStatusesCount())); 
			    this.tVFansCount.setText(String.valueOf(user.getFollowersCount()));  
			    this.tvFavCount.setText(String.valueOf(user.getFavouritesCount())); 
			    
			    
			    if(MainService.allIcon.get(MainService.mainService.currentUser.getId())!=null){	
			    	this.ivPortrait.setImageBitmap(MainService.allIcon.get(MainService.mainService.currentUser.getId()));
			    }else
		        {// 设定缺省的图片
		        	this.ivPortrait.setImageResource(R.drawable.portrait);	
		        	HashMap tashParam = new HashMap();
		        	tashParam.put("uid", user.getId());
		        	tashParam.put("url", user.getProfileImageURL());
		        	Task task = new Task(Task.TASK_GET_USER_ICON_4_USERINFO, tashParam);
		        	MainService.newTaskToTaskQueneHead(task);
		        	
		        }
			   
			   
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
			
		case REFRESH_USERICON://加载用户图标
			if(MainService.allIcon.get(MainService.mainService.currentUser.getId())!=null){	
		    	this.ivPortrait.setImageBitmap(MainService.allIcon.get(MainService.mainService.currentUser.getId()));
		    }
			break;
		default:
			break;
		}
		
	}
    
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d("keydown", ".................." + keyCode);
		if (keyCode == 4)// 如果用户按下了返回键
		{
			MainService.promptExitApp(this);
		}
		return true;
	}
	
	@Override
	public void initControlsAndAddControlsEvent() {
		// TODO Auto-generated method stub
		
	}
}
